package com.demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.module.commonui.R;
import com.tg.appcommon.android.LanguageUtils;
import com.tg.appcommon.android.TGLog;


public class TGFocusingView extends RelativeLayout implements View.OnClickListener, View.OnTouchListener, View.OnLongClickListener {
    public static final String TAG = "TGFocusingView";
    protected ImageView topButton, bottomButton;
    private OnFocusTouchListener onFocusTouchListener;
    public static final int TCIC_LENS_FOCAL_NEAR = 25;
    public static final int TCIC_LENS_FOCAL_FAR = 26;
    protected int ptzCmd = -1;
    protected boolean isSendPTZCmd = false;
    public void setFocusTouchListener(OnFocusTouchListener onFocusTouchListener){
        this.onFocusTouchListener = onFocusTouchListener;
    }
    public TGFocusingView(Context context) {
        super(context);
        initContentView(context);
    }

    public TGFocusingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TGFocusingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initContentView(context);
    }

    public void setSendFocusCmd(boolean sendPTZCmd) {
        isSendPTZCmd = sendPTZCmd;
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initContentView(Context context) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.layout_focus, this, true);
        topButton = convertView.findViewById(R.id.iv_zoom_far);
        bottomButton = convertView.findViewById(R.id.iv_zoom_near);
        topButton.setOnClickListener(this);
        topButton.setOnTouchListener(this);
        topButton.setOnLongClickListener(this);


        TextView textView =  convertView.findViewById(R.id.tv_focus_tip);
        if (LanguageUtils.isSimChinese(context)){
            textView.setVisibility(VISIBLE);
        }else{
            textView.setVisibility(GONE);
        }

        bottomButton.setOnClickListener(this);
        bottomButton.setOnTouchListener(this);
        bottomButton.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        TGLog.d(TAG, "onClick id = " + id);
        if (id == R.id.iv_zoom_far){
            sendShortCmd(1);
        }else if (id == R.id.iv_zoom_near){
            sendShortCmd(-1);
        }
    }

    private void sendShortCmd(int cmd) {
        if (onFocusTouchListener != null) {
            if (isSendPTZCmd) {
                TGLog.d(TAG, "onStopCmd ptzCmd");
                onFocusTouchListener.onStopCmd();
            } else{
                onFocusTouchListener.onShortCmd(cmd);
            }

            isSendPTZCmd = false;
            TGLog.d("PtzControlView", "sendShortCmd = " + cmd);
            ptzCmd = -1;
        }
    }

    @Override
    public boolean onLongClick(View view) {
        TGLog.d(TAG, "onLongClick");
        int id = view.getId();
        TGLog.d(TAG, "onClick id = " + id);
        if (id == R.id.iv_zoom_far){
            sendLongCmd(TCIC_LENS_FOCAL_FAR);
        }else if (id == R.id.iv_zoom_near){
            sendLongCmd(TCIC_LENS_FOCAL_NEAR);
        }

        return false;
    }

    private void sendLongCmd(int cmd) {
        if (onFocusTouchListener != null) {
            if (ptzCmd == -1) {
                ptzCmd = cmd;
                onFocusTouchListener.onLongCmd(ptzCmd);
            } else if (cmd != ptzCmd) {
                ptzCmd = cmd;
                onFocusTouchListener.onStopCmd();
                onFocusTouchListener.onLongCmd(ptzCmd);
            }
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        int id = view.getId();
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
            int resId = R.drawable.btn_background_white_top_gray_r10;
            if (id == R.id.iv_zoom_near){
                resId = R.drawable.btn_background_white_bottom_gray_r10;
            }
            view.setBackgroundResource(resId);
        }else if (motionEvent.getAction() == MotionEvent.ACTION_UP){
            int resId = R.drawable.btn_background_white_top_r10;
            if (id == R.id.iv_zoom_near){
                resId = R.drawable.btn_background_white_bottom_r10;
            }
            view.setBackgroundResource(resId);
        }

        if (onFocusTouchListener != null){
            onFocusTouchListener.onTouch(motionEvent.getAction() == MotionEvent.ACTION_UP, id == R.id.iv_zoom_far);
        }
        return false;
    }


    public interface OnFocusTouchListener {
        void onShortCmd(int control);
        void onStopCmd();
        void onLongCmd(int control);
        void onTouch(boolean up, boolean add);
    }
}
